<template>
	<view>
		<u-popup :show="showWxLogin" mode="bottom" round="44rpx" :closeable="true" @close="onClose">
			<view class="popupBox">
				<view class="h2 bold">获取您的昵称、头像</view>
				<view class="h3">获取用户头像、昵称，主要用于向用户提供具有辨识度的个人主页、捐资显示等</view>
				<view class="form">
					<view class="input u-flex">
						<view class="h2">头像</view>
						<view class="info">
							<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
								<image mode="aspectFill" class="avatar"
									:src="avatarUrl || '../static/image/icon_head_portrait.png'"></image>
							</button>
							
						</view>
						<image class="right-img" src="../static/image/right.png" mode="aspectFit">
						</image>
					</view>
					<view class="input u-flex">
						<view class="h2">昵称</view>
						<view class="info">
							<input type="nickname" v-model="nickName" placeholder="请输入昵称" @blur="onBlur" />
						</view>
					</view>
				</view>
				<view class="submit" @click="submit">确定</view>
			</view>
		</u-popup>
	</view>
</template>
<script>
	// import {
	// 	uploadAvatarApi
	// } from '@/common/api.js'
	export default
	{
		props:
		{
			showWxLogin:
			{
				type: Boolean,
				required: true,
			}
		},
		data()
		{
			return {
				PrimaryColor: '#1fba1a', //主题色
				avatarUrl: uni.getStorageSync('Avatar'),
				nickName: uni.getStorageSync('wxName')
			}
		},
		methods:
		{
			onClose()
			{
				this.$emit('onClose')
			},
			onChooseAvatar(e)
			{
				uni.compressImage(
				{
					src: e.detail.avatarUrl,
					quality: 80,
					success: res =>
					{
						console.log(res);
						// this.avatarUrl = res.tempFilePath
						this.handleUploadAvatar(res.tempFilePath)
					}
				})
			},
			async handleUploadAvatar(filePath)
			{
				try
				{
					uni.uploadFile(
					{
						url: 'https://api.mingway.info/ai/api/uploadFile',
						filePath: filePath,
						name: 'file',
						success: uploadFileRes =>
						{
							console.log('上传图片', JSON.parse(uploadFileRes.data));
							this.avatarUrl = JSON.parse(uploadFileRes.data).data.fileUrl
							uni.setStorageSync('Avatar', this.avatarUrl)
						},
						fail(err)
						{
							console.log(err);
						}
					});
					// const res = await this.$post('api/uploadFile', { file: formData })
					// //  uploadAvatarApi({
					// // 	filePath: filePath,
					// // 	name: 'file'
					// // })
					// if (res.code === 200)
					// {
					// 	this.avatarUrl = res.data
					// }
				}
				catch (e)
				{
					uni.showToast(
					{
						title: '头像上传失败~',
						icon: 'none'
					});
				}
			},
			onBlur(e)
			{
				
				this.nickName = e.detail.value
			},
			submit()
			{
				if (!this.avatarUrl)
				{
					uni.showToast(
					{
						title: '请上传头像~',
						icon: 'none'
					});
					return
				}
				if (!this.nickName)
				{
					uni.showToast(
					{
						title: '请输入昵称~',
						icon: 'none'
					});
					return
				}
				let userInfo = {
					avatar_url: this.avatarUrl,
					nick_name: this.nickName,
				}
			
				this.$emit("setUserInfoCallback", userInfo) //全局登录监听回调方法
			},
		},
	}
</script>
<style lang="scss" scoped>
	.popupBox {
		width: 750rpx;
		box-sizing: border-box;
		padding: 50rpx 40rpx 30rpx;

		.h2 {
			color: #333;
			font-size: 32rpx;
		}

		.h3 {
			font-size: 26rpx;
			color: #999;
			line-height: 1.4;
			padding-top: 14rpx;
		}

		.bold {
			font-weight: bold;
		}

		.form {
			margin-top: 30rpx;
			border-top: 1px solid #EFEFEF;

			.input {
				display: flex;
				padding: 24rpx 0;
				border-bottom: 1px solid #EFEFEF;
				align-items: center;
				justify-content: space-between;

				.info {
					flex: 1;
					margin-left: 32rpx;

					.avatar-wrapper {
						width: 70rpx;
						height: 70rpx;
						border-radius: 10rpx;
						padding-left: 0 !important;
						margin-left: 0 !important;

						.avatar {
							width: 70rpx;
							height: 70rpx;
							border-radius: 10rpx;
						}
					}

					button::after {
						border: none;
					}

					input {
						background: transparent;
						width: 100%;
						height: 70rpx;
						color: #333;
						font-size: 30rpx;
					}
				}

				.right-img {
					width: 24rpx;
					height: 24rpx;
				}
			}
		}
	}

	.submit {
		margin-top: 30rpx;
		width: 100%;
		border-radius: 10rpx;
		text-align: center;
		line-height: 92rpx;
		color: #fff;
		font-size: 32rpx;
		height: 88rpx;
		background: linear-gradient(222deg, #2CE6FF 0%, #2A87FF 100%);
		box-shadow: 0px 4rpx 16rpx 0px rgba(0, 0, 0, 0.08);
		border-radius: 44rpx;
	}
</style>
